package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    private static Map<Integer,String> typeMap;

    static {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"username");
        map.put(2,"phone");
        map.put(3,"email");
        typeMap = map;
    }


    /**
     * 查询数据库 检查是否有数据
     * @param param
     * @param type
     * @return
     */
    @Override
    public boolean checkUser(String param, Integer type) {
//        String column = (type==1)?"username":(type==2?"phone":"email";
//        String column = null;
//        if(type == 1){
//            column = "username";
//        }else if(type == 2){
//            column = "phone";
//        }else{
//            column = "email";
//        }
        //1.根据参数类型获取校验的类型 column
        String column = typeMap.get(type);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column,param);
        int count = userMapper.selectCount(queryWrapper);
        return count>0?true:false;
    }

    @Override
    public void saveHttpCleint(User userPOJO) {
        userPOJO.setEmail("111222333@qq.com") //数据库中要求email和phone是非空的 暂时写死
                .setPhone("1311112222");
        userMapper.insert(userPOJO);
    }
}
